package com.sixkey.service.impl;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.sixkey.constant.SendSmsConstant;
import com.sixkey.service.IAsyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: AsyncServiceImpl
 * Package: com.sixkey.service.impl
 * Description:
 *
 * @Author: @weixueshi
 * @Create: 2023/10/12 - 23:51
 * @Version: v1.0
 */

/**
 * 异步发送会议通知消息
 */
@Slf4j
@Service
public class AsyncServiceImpl implements IAsyncService {

    /**
     * 发送短信
     * @param
     * @param
     */
    @Async("asyncExecutor")
    public void sendSuccessMessage(List<String> participants, String phone, String time){
       participants.stream().forEach(item->{
           DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", SendSmsConstant.ACCESS_KEY_ID, SendSmsConstant.ACCESS_KEY_SECRET);
           IAcsClient client = new DefaultAcsClient(profile);

           SendSmsRequest request = new SendSmsRequest();
           request.setSysRegionId("cn-hangzhou");
           request.setPhoneNumbers(item);
           /**
            * 签名
            */
           request.setSignName("文学博客");
           /**
            * 模板
            */
           request.setTemplateCode("SMS_463677677");
           request.setTemplateParam("{\"phone\":\""+phone+"\",\"time\":\""+time+"\"}");
           try {
               client.getAcsResponse(request);
           } catch (ClientException e) {
               throw new RuntimeException(e);
           }
           log.info("短信发送成功");
       });
    }

    /**
     * 会议取消通知
     * @param participants
     * @param phone
     * @param time
     */
    @Override
    public void sendCancelMessage(List<String> participants, String phone, String time) {
        participants.stream().forEach(item->{
            DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", SendSmsConstant.ACCESS_KEY_ID, SendSmsConstant.ACCESS_KEY_SECRET);
            IAcsClient client = new DefaultAcsClient(profile);

            SendSmsRequest request = new SendSmsRequest();
            request.setSysRegionId("cn-hangzhou");
            request.setPhoneNumbers(item);
            /**
             * 签名
             */
            request.setSignName("文学博客");
            /**
             * 模板
             */
            request.setTemplateCode("SMS_463677677");
            request.setTemplateParam("{\"phone\":\""+phone+"\",\"time\":\""+time+"\"}");
            try {
                client.getAcsResponse(request);
            } catch (ClientException e) {
                throw new RuntimeException(e);
            }
            log.info("短信发送成功");
        });
    }

    /**
     * 会议延长通知
     * @param participants
     * @param phone
     * @param time
     */
    @Override
    public void sendAddLengthMessage(List<String> participants, String phone, String time) {
        participants.stream().forEach(item->{
            DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", SendSmsConstant.ACCESS_KEY_ID, SendSmsConstant.ACCESS_KEY_SECRET);
            IAcsClient client = new DefaultAcsClient(profile);

            SendSmsRequest request = new SendSmsRequest();
            request.setSysRegionId("cn-hangzhou");
            request.setPhoneNumbers(item);
            /**
             * 签名
             */
            request.setSignName("文学博客");
            /**
             * 模板
             */
            request.setTemplateCode("SMS_463677677");
            request.setTemplateParam("{\"phone\":\""+phone+"\",\"time\":\""+time+"\"}");
            try {
                client.getAcsResponse(request);
            } catch (ClientException e) {
                throw new RuntimeException(e);
            }
            log.info("短信发送成功");
        });
    }
}
